﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Trade.Model;

namespace Trade.DAL
{
    public class Invoice
    {
        //创建临时数据库存在当前Invoice的明细资料行
        public static DataTable InvoiceDetailTemplate()
        {
            string cmdString = "Select * from InvoiceDetail where 1 > 2";

            using (SqlDataReader rdr = SQLHelper.ExecuteReader(SQLHelper.connectionString, CommandType.Text, cmdString, null))
            {
                return Utility.ConvertToDataTable(rdr);
            }
        }

        //生成新的发票编号
        public static string GenInvoiceNo(string curDate)
        {
            string cmdString = "exec Usp_SerialNo @p_SerialNoName, @p_FieldName, @p_TableName, @p_PrefixTime, @p_RSerialNo out";
            string invoiceNo = "";

            SqlParameter[] parms = {
                new SqlParameter("@p_SerialNoName", SqlDbType.VarChar, 30),
                new SqlParameter("@p_FieldName", SqlDbType.VarChar, 30),
                new SqlParameter("@p_TableName", SqlDbType.VarChar, 30),
                new SqlParameter("@p_PrefixTime", SqlDbType.VarChar, 8),
                new SqlParameter("@p_RSerialNo", SqlDbType.VarChar, 30)};

            parms[0].Value = "SerialNo";
            parms[1].Value = "InvoiceNo";
            parms[2].Value = "InvoiceMaster";
            parms[3].Value = curDate.Trim();
            parms[4].Value = invoiceNo;
            parms[4].Direction = ParameterDirection.InputOutput;

            SQLHelper.ExecuteNonQuery(SQLHelper.connectionString, CommandType.Text, cmdString, parms);
            invoiceNo = parms[4].Value.ToString();

            return invoiceNo;
        }

        public void Modify(InvoiceMasterInfo invoiceMaster, DataTable dtInvoiceDetail)
        {

        }

        private static SqlParameter[] SetInvoiceMasterParameters(InvoiceMasterInfo invoiceMaster, string userId)
        {
            SqlParameter[] parms = {
                new SqlParameter("@InvoiceNo", SqlDbType.VarChar, 30),
                new SqlParameter("@CustomerNo", SqlDbType.VarChar, 8), 
                new SqlParameter("@VendorNo", SqlDbType.VarChar, 8),
                new SqlParameter("@ProductAmount", SqlDbType.Decimal, 15),
                new SqlParameter("@TaxAmount", SqlDbType.Decimal, 15), 
                new SqlParameter("@InvoiceDate", SqlDbType.DateTime), 
                new SqlParameter("@Recipient", SqlDbType.VarChar, 20), 
                new SqlParameter("@Review", SqlDbType.VarChar, 20), 
                new SqlParameter("@Drawer", SqlDbType.VarChar, 20),
                new SqlParameter("@ReferenceNo", SqlDbType.VarChar, 30), 
                new SqlParameter("@Remark", SqlDbType.VarChar, 60),
                new SqlParameter("@InvoiceType", SqlDbType.Int),
                new SqlParameter("@RecordId", SqlDbType.Int),
                new SqlParameter("@UserId", SqlDbType.VarChar, 15), 
                new SqlParameter("@ErrMsg", SqlDbType.VarChar, 100),
                new SqlParameter("@RFlag", SqlDbType.SmallInt)};

            parms[0].Value = invoiceMaster.InvoiceNo.Trim();
            parms[1].Value = invoiceMaster.CustomerNo.Trim();
            parms[2].Value = invoiceMaster.VendorNo.Trim();
            parms[3].Value = invoiceMaster.ProductAmount;
            parms[4].Value = invoiceMaster.TaxAmount;
            parms[5].Value = invoiceMaster.InvoiceDate;
            parms[6].Value = invoiceMaster.Recipient.Trim();
            parms[7].Value = invoiceMaster.Review.Trim();
            parms[8].Value = invoiceMaster.Drawer.Trim();
            parms[9].Value = invoiceMaster.ReferenceNo.Trim();
            parms[10].Value = invoiceMaster.Remark.Trim();
            parms[11].Value = invoiceMaster.InvoiceType;
            parms[12].Value = invoiceMaster.RecordId;
            parms[12].Direction = ParameterDirection.InputOutput;
            parms[13].Value = userId.Trim();
            parms[14].Value = "";
            parms[14].Direction = ParameterDirection.InputOutput;
            parms[15].Value = 0;
            parms[15].Direction = ParameterDirection.InputOutput;

            return parms;
        }

        private static SqlParameter[] SetInvoiceDetailParameters(InvoiceDetailInfo invoiceDetail, string userId)
        {
            SqlParameter[] parms = {
                new SqlParameter("@InvoiceNo", SqlDbType.VarChar, 30),
                new SqlParameter("@LineNo", SqlDbType.VarChar, 3), 
                new SqlParameter("@ProductNo", SqlDbType.VarChar, 10),
                new SqlParameter("@Qty", SqlDbType.Decimal, 10), 
                new SqlParameter("@Price", SqlDbType.Decimal, 10), 
                new SqlParameter("@Amount", SqlDbType.Decimal, 15), 
                new SqlParameter("@TaxRate", SqlDbType.Decimal, 10), 
                new SqlParameter("@TaxAmount", SqlDbType.Decimal, 15),
                new SqlParameter("@Remark", SqlDbType.VarChar, 60),
                new SqlParameter("@RecordId", SqlDbType.Int),
                new SqlParameter("@UserId", SqlDbType.VarChar, 15), 
                new SqlParameter("@ErrMsg", SqlDbType.VarChar, 100),
                new SqlParameter("@RFlag", SqlDbType.SmallInt)};

            parms[0].Value = invoiceDetail.InvoiceNo.Trim();
            parms[1].Value = invoiceDetail.LineNo.Trim();
            parms[2].Value = invoiceDetail.ProductNo.Trim();
            parms[3].Value = invoiceDetail.Qty;
            parms[4].Value = invoiceDetail.Price;
            parms[5].Value = invoiceDetail.Amount;
            parms[6].Value = invoiceDetail.TaxRate;
            parms[7].Value = invoiceDetail.TaxAmount;
            parms[8].Value = invoiceDetail.Remark.Trim();
            parms[9].Value = invoiceDetail.RecordId;
            parms[9].Direction = ParameterDirection.InputOutput;
            parms[10].Value = userId.Trim();
            parms[11].Value = "";
            parms[11].Direction = ParameterDirection.InputOutput;
            parms[12].Value = 0;
            parms[12].Direction = ParameterDirection.InputOutput;

            return parms;
        }
    }
}
